package com.soc.erp.index;
import javax.servlet.http.HttpServletRequest;
import org.nature.framework.annotation.GlobalInterceptor;
import org.nature.framework.core.NatureCtrl;
import org.nature.framework.interceptor.Invocation;
import org.nature.framework.interceptor.NatureInterceptor;

/**
 * 拦截器事例（非必须）
 * 拦截器可以通过@Interceptors注解来定义到类或者方法上
 */
@GlobalInterceptor
public class GlobInterceptor implements NatureInterceptor{
	@Override
	public void intercept(Invocation invocation) {
		Boolean bSuccess = false;
		HttpServletRequest request = invocation.getRequest();
		String current_url = request.getServletPath();
		//System.out.println("current_url="+current_url);
		//invocation.invoke(); 
		if ("/index/writeImgae".equals(current_url)||"/index/login".equals(current_url)||"/index/checkout".equals(current_url))
		{
			bSuccess = true;
		}
		else{
			NatureCtrl ctrl = (NatureCtrl) invocation.getTargetObject();
			String loginuser = (String) ctrl.getSession().getAttribute("login_name");
			if (loginuser != null)
				bSuccess = true;
		}
		if (bSuccess) {
			invocation.invoke();
		} else {
			System.err.println("拦截  url: " + current_url);
			invocation.setReturnValue("/index/login");
		}
	}

	@Override
	public int level() {return 0;}
}
